// ==============================================================================
//
// software development kit v 3.0
// input unit
//
// copyright (c) 2008-2009 gear pair games
//
// copyright and license exceptions noted in source
//
// ==============================================================================
unit renderApi;

interface

uses
  types,
  headers,
  structs,
  math,
  enums,
  consts;

type

// CRenderableApi
  CRenderableApi = class( IRenderableApi )
  private
    // os handle-context
    fHandle : handle;
    fProp : vec4i;
    fDrawApi : IDrawCustomApi;
  protected { protected }
    // CRenderableApi::getBuffer
    function getBuffer : IRenderBuffer; override;
    // CRenderableApi::getDrawApi
    function getDrawApi : IDrawCustomApi; override;
    // CRenderableApi::getWndHandle
    function getWndHandle : handle; override;
    // CRenderableApi::getFacotryCam
    function getFacotryCam : ICameraFactory; override;
  public
    // basic function
    // CRenderableApi::doInit
    procedure doInit; override;
  public { public }
    // CRenderableApi::buffer
    property buffer : IRenderBuffer read getBuffer;
    // CRenderableApi::drawApi
    property drawApi : IDrawCustomApi read getDrawApi;
    // CRenderableApi::wndHandle
    property wndHandle : handle read getWndHandle;
    // CRenderableApi::screenPos
    function screenPos : vec2i; override;
    // CRenderableApi::screenSize
    function screenSize : vec2i; override;
    // CRenderableApi::factoryCam
    property factoryCam : ICameraFactory read getFacotryCam;
    // CRenderableApi::reset
    procedure reset; override;
  end;

implementation

uses
  cls,
  cameraApi,
  xml,
  os,
  utils,
  msg,
  html;

{ CRenderApi }

// CRenderApi::getWndHandle
procedure CRenderableApi.doInit;
const
  msgErrRender = 'error, not find lib-render api:';
  msgErrReturnHandleApi = 'error, need return handle api';
  msgApiCaption = 'current api : ';
  msgHintXmlLoad = 'file xml has been load and parsed : ';
  msgWndFail = 'wnd create fail handle = null';
var
  xmlCfg : CXML;
  apiName : AnsiString;
begin
// init
  addChild( CCameraFactory.Create );
// load xml config
  xmlCfg := CXML.Create( INotifier( notifier ).getPath + AnsiString( xmlCfgFile ) );
// log load xml file
  printf( logHint, msgHintXmlLoad + xmlCfgFile );
  fProp.size.height := xmlCfg.Params[ xmlCfgHeight ].asInt;
  fProp.size.width := xmlCfg.Params[ xmlCfgWidth ].asInt;
  apiName := xmlCfg.Node[ xmlCfgPipelineName ].Params[ xmlCfgPipelineApi ].asText;
  printf( logHint, msgApiCaption + htmb + apiName + htmbb );
// init wnd
  fHandle := osWndCreate( fProp );
  exception( fHandle <> nullInt, msgWndFail );
// load default GAPI
  fDrawApi := IDrawCustomApi( INotifier( notifier ).getChildByName( apiName, true ) );
  exception( fDrawApi <> nil, msgErrRender + apiName );
  exception( fDrawApi.initRender( xmlCfg, self ), msgErrReturnHandleApi );
// init render
// fDrawApi.writeInitStatus;
// xml - free
  exception( osWndShow( fHandle ), '#' );
  // free xml-cfg
  xmlCfg.Free;
  // set draw view
  fDrawApi.setView( fProp.size.width, fProp.size.height );
  // inherited call
  inherited;
end;

// CRenderableApi::getWndHandle
function CRenderableApi.getBuffer : IRenderBuffer;
const
  intf : IRenderBuffer = nil;
begin
  if intf = nil then
    PCObject( @intf )^ := getChildByMeta( IRenderBuffer );
  result := intf;
end;

// CRenderableApi::getWndHandle
function CRenderableApi.getDrawApi : IDrawCustomApi;
begin
  result := fDrawApi;
end;

// CRenderableApi::getWndHandle
function CRenderableApi.getFacotryCam : ICameraFactory;
const
  intf : ICameraFactory = nil;
begin
  if intf = nil then
    PCObject( @intf )^ := getChildByMeta( ICameraFactory );
  result := intf;
end;

// CRenderableApi::getWndHandle
function CRenderableApi.getWndHandle : handle;
begin
  result := fHandle;
end;

// CRenderableApi::reset
procedure CRenderableApi.reset;
begin
end;

// CRenderableApi::screenPos
function CRenderableApi.screenPos : vec2i;
begin
  result := fProp.pos;
end;

// CRenderableApi::screenSize
function CRenderableApi.screenSize : vec2i;
begin
  result := fProp.size;
end;

end.
